package com.gjy.kafka.k11.consumer;

import com.gjy.kafka.k11.KafkaConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-11-03 14:49:31
 */
public class StreamsConsumer {
    private static final Logger log = LoggerFactory.getLogger(StreamsConsumer.class);

    public static void main(String[] args) throws InterruptedException {
        Properties properties = KafkaConfig.streamsConsumer("streams-consumer");
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);
        List<String> topics = Collections.singletonList("streams-word-output");
        consumer.subscribe(topics);
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(1));
            for (ConsumerRecord<String, String> record : records) {
                log.info("接收到消息: key = {}, value = {}, partition = {}, offset = {}",
                        record.key(), record.value(), record.partition(), record.offset());
            }
        }
    }

}
